home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 27 Feb 1996 11:09:11 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gvksnINNnug@anvil.ugrad.cs.ubc.ca>
- References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu> <4gtab6$acb@ceylon.gte.com>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4gtab6$acb@ceylon.gte.com>, Brenda <g051286> wrote:
- >schlein@umbc.edu (Jonas J. Schlein) wrote:
- >>Abu Wawda <wawda@alcor.usc.edu> wrote:
- >>|> I'm having trouble understanding what the address of a static array
- >>|> is.
- >>
- >>I'm having trouble understanding why it matters? You almost never use the
- >>address of an array directly unless doing something tricky with pointers
- >>or with particular dimensions of a multiple dimensional array.
- >>
- >>|> For example, if I declare a variable called myarray as:
- >>|> char myarray[10];
- >>|> then what could &myarray possibly mean? myarray is not a pointer, so
- >>|> &myarray could not possibly be the address of the variable myarray
- >>|> (like it would be if I did char* myarray and then asked for &myarray).
- >>
- >>Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
- >>a pointer to 'myarray'.
- >
- >Um, that's not correct. myarray is DEFINITELY a pointer! As declared above,
-
- False. The _name_ 'myarray' is an array object. The _expression_ 'myarray'
- produces a pointer.
-
- >it is a constant pointer to 10 contiguous char datatypes. myarray is an
-
- It is not a constant pointer. I don't see a 'const' keyword anywhere, and it
- is not a pointer to begin with. You can't make 'myarray' point somewhere else
- by initialization. True const pointers can be _initialized_ to point
- to an arbitrary object (of compatible type), either through a parameter pass,
- static initialization or automatic initialization.
-
- Secondly, the type of the expression 'myarray' is not a pointer to 10
- datatypes. It is a pointer to one character. A 'char *'. A pointer to 10
- contiguous characters is 'char (*)[10]'.
-
- >ADDRESS whereas *(myarray + 5) or myarray[5] is the 6th element in the array.
- >The difference between an array and something like "char *p=myarray", is that
- >you can say p++, but you can't say myarray++. You shouldn't say &myarray
- >either because myarray is a constant, but I read that on some compilers
- >scanf ignores the dereferencing and does not bother to warn you.
-
- This is false. ANSI gives meaning to applying the & operator to an array.
- You are way off base. The result of applying & to an array is a pointer to the
- whole array. In this case, it is a char (*)[10] or pointer to an array of ten
- characters.
-
- This operation is important. It allows you to do this:
-
- typedef char des_cblock[8];
-
- and thereafter treat "des_cblock" as a type which holds eight characters. You
- can have functions that take a "des_cblock *key" argument, and the compiler
- can typecheck that against calling with a wrong array type.
-
- The above typedef is from a well known freeware DES encryption library, LibDES.
- It effectively hides the fact that a key is an array of eight blocks. It could
- easily be changed to a structure, and the client programs wouldn't know the
- difference, since the library calls take _pointers_ to des_cblock. If they did
- not, and you changed from arrays to structures, you would be changing the
- passing of a pointer to the first element of an array (effectively a pass of
- the array object by reference) to a pass by value of a structure, thus failing
- to preserve the semantics of the library calls.
-
- I find such typedefs very useful. For example, in some programs I have certain
- character fields that appear in more than one structure type. For example, a
- customer identification code (say 10 characters) can appear in an invoice or in
- a customer record. If you do this:
-
- typedef char cust_code_t[10];
-
- You can then declare structure members of type 'cust_code_t' in various
- structures, and use a pointers to 'cust_code_t' in functions that expect a
- customer ID parameter.
-
- Instead of messy #defines for field sizes, I can just use
- sizeof(cust_code_t) to get the size, as I would if it were a struct, union or
- anything else.
-
-
-
-
- pointer. If you use
-
-
-
-
-
-
-
-
- --
-
-